home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / powervww / pvfc.cpp < prev    next >
C/C++ Source or Header  |  1998-01-05  |  5KB  |  161 lines

  1. //  ____________________________________________________
  2. // |                                                    |
  3. // |  Project:     POWER VIEW INTERFACE                 |
  4. // |  File:        PVFC.CPP                             |
  5. // |  Compiler:    WPP386 (10.6)                        |
  6. // |                                                    |
  7. // |  Subject:     Power View Font Compiler             |
  8. // |                                                    |
  9. // |  Author:      Emil Dotchevski                      |
  10. // |____________________________________________________|
  11. //
  12. // E-mail: zajo@geocities.com
  13. // URL:    http://www.geocities.com/SiliconValley/Bay/3577
  14.  
  15. #define uses_stdlib
  16. #define uses_dos
  17. #define uses_fcntl
  18. #define uses_stdio
  19. #define uses_basics
  20. #define uses_comlin
  21. #define uses_system
  22.  
  23. #include "PVuses.h"
  24.  
  25. char font1[256*16], font2[256*16];
  26. Tset graph_chars;
  27. word graph_count;
  28. word char_size = 16;
  29.  
  30. void error( int flag, char *s, char *m )
  31. {
  32.   if( flag )
  33.   {
  34.     printf( "Error: %s %s", s, m );
  35.     exit( -1 );
  36.   }
  37. }
  38.  
  39. boolean read_font( char *filename, char *font )
  40. {
  41.   int handle;
  42.   unsigned num_read;
  43.  
  44.   if( _dos_open( filename, O_RDONLY, &handle) ) return 0;
  45.   if( _dos_read( handle, font, 256*16, &num_read ) || ( num_read != 256*16 ) )
  46.   {
  47.     _dos_close( handle );
  48.     return 0;
  49.   }
  50.   _dos_close( handle );
  51.   return 1;
  52. }
  53.  
  54. boolean cmp_chars( word chr )
  55. {
  56.   word i;
  57.  
  58.   for( i = 0; i < char_size; i++ )
  59.     if( font1[16*chr+i] != font2[16*chr+i] ) return 0;
  60.   return 1;
  61. }
  62.  
  63. void cmp_fonts( void )
  64. {
  65.   word i;
  66.  
  67.   graph_count = 0;
  68.   for( i = 0; i < 256; i++ )
  69.     if( !cmp_chars( i ) )
  70.     {
  71.       graph_chars<<i;
  72.       graph_count++;
  73.     }
  74. }
  75.  
  76. boolean write_cpp( char *filename, char *font )
  77. {
  78.   FILE *f;
  79.   unsigned num_read;
  80.   word i, c, cnt;
  81.  
  82.   if( !( f = fopen( filename, "w+" ) ) ) return 0;
  83.   if( fprintf(f,"\n#define GRAPH_COUNT%d %d\n", char_size, graph_count ) == EOF ) return 0;
  84.   if( fprintf( f, "char graph_set%d[32] = {", char_size ) == EOF ) return 0;
  85.   for( i = 0; i < 32; i++ )
  86.   {
  87.     if( !( i % 8 ) && ( fprintf( f, "\n  " ) == EOF ) ) return 0;
  88.     if( fprintf( f, "0x%02X", graph_chars.members[i] ) == EOF ) return 0;
  89.     if( ( i < 31 ) && ( fprintf( f, "," ) == EOF ) ) return 0;
  90.   }
  91.   if( fprintf( f,"\n};\nchar graph_chars%d[GRAPH_COUNT%d][%d] = {\n", char_size, char_size, char_size ) == EOF ) return 0;
  92.   cnt = 0;
  93.   for( i = 0; i < 256; i++ )
  94.     if( i&graph_chars )
  95.     {
  96.       if( fprintf( f, "  { " ) == EOF ) return 0;
  97.       for( c = 0; c < char_size - 1; c++ )
  98.         if( fprintf( f, "0x%02X,", font[16*i+c] ) == EOF ) return 0;
  99.       if( fprintf( f, "0x%02X }", font[16*i+char_size-1] ) == EOF ) return 0;
  100.       if( ++cnt < graph_count )
  101.       {
  102.         if( fprintf( f,",\n" ) == EOF ) return 0;
  103.       }
  104.       else
  105.         if( fprintf( f, "\n" ) == EOF ) return 0;
  106.     }
  107.   if( fprintf(f,"};\n") == EOF ) return 0;
  108.   fclose( f );
  109.   return 1;
  110. }
  111.  
  112. char fn1[_MAX_PATH], fn2[_MAX_PATH], fn3[_MAX_PATH];
  113.  
  114. void proceed_command_line( void )
  115. {
  116.   printf("Power View Font Compiler. Written by Emil Dochevsky.\n\n");
  117.   if( !param_filename( fn1, ".FNT" ) ||
  118.       !param_filename( fn2, ".CPP" ) ||
  119.       param_opt( "/?" ) )
  120.   {
  121.     printf( "\
  122. Syntax:\n\
  123.   fnt2c <font_file>[.FNT] <C_file>[.CPP] [<standard_font>[.FNT]] [/<x>]\n\n\
  124. Description:\n\
  125.   If <standard_font> is omitted, complete font definition is exported.\n\
  126.   Otherwise <standard_font> is compared to <font_file> and only different\n\
  127.   chars definitions are exported.\n\
  128.   <x> is bytes per char to compare/export (default=16)\n"
  129.     );
  130.     exit( 1 );
  131.   }
  132.   *fn3 = 0; param_filename( fn3, ".FNT" );
  133.   if( param_opt( "/8" ) ) char_size = 8;
  134.   if( param_opt( "/14" ) ) char_size = 14;
  135.   if( param_opt( "/16" ) ) char_size = 16;
  136. }
  137.  
  138. word i;
  139.  
  140. void main( int argc, char* argv[] )
  141. {
  142.   __init_comlin( argc, argv );
  143.   proceed_command_line();
  144.   __tini_comlin();
  145.   printf( "Reading first font file...\n" );
  146.   error( !read_font( fn1, font1 ), "Error reading first font file", fexpand( fn1 ) );
  147.   if( *fn3 == 0 )
  148.     for( i = 0; i < 256*16; i++ )
  149.       font2[i] = ~font1[i];
  150.   else
  151.   {
  152.     printf( "Reading second font file...\n" );
  153.     error( !read_font( fn3, font2 ), "Error reading second font file", fexpand( fn3 ) );
  154.     printf( "Comparing chars definitions...\n" );
  155.   }
  156.   cmp_fonts();
  157.   printf( "Writing output file...\n" );
  158.   error( !write_cpp( fn2, font1 ), "Error writing C file", fexpand( fn2 ) );
  159.   printf( "Done.\n\nOutput file: %s\n%d chars definitions exported.\n", fexpand( fn2 ), graph_count );
  160. }
  161.